package com.thinkin.services.ordercenter;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.allDo.SysDao;
import com.thinkin.ordercenter.vo.OonlineShopVo;
import com.thinkin.util.ExtendDate;
import com.thinkin.util.JsonHelper;

public class OonlineShop {

	private Logger log = LoggerFactory.getLogger(this.getClass().getName());

	public String add(String jsonstr) {
		String keyid = "";
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		com.thinkin.ordercenter.po.OonlineShop oonlineShop = new com.thinkin.ordercenter.po.OonlineShop();

		try {
			keyid = UUID.randomUUID().toString();
			oonlineShop.setKeyid(keyid);
			oonlineShop.setChannelKeyid(jsonObject.get("channelKeyid")
					.toString());
			oonlineShop.setPlatformID(jsonObject.get("platformID").toString());
			oonlineShop
					.setNetshopType(jsonObject.get("netshopType").toString());
			oonlineShop.setNetShopUserName(jsonObject.get("netShopUserName")
					.toString());
			oonlineShop
					.setNetShopName(jsonObject.get("netShopName").toString());
			oonlineShop.setNetShopUrl(jsonObject.get("netShopUrl").toString());
			// oonlineShop.setFileQueue(jsonObject.get("fileQueue").toString());
			oonlineShop.setAppKey(jsonObject.get("appKey").toString());
			oonlineShop.setAppSecret(jsonObject.get("appSecret").toString());
			oonlineShop.setAppSession(jsonObject.get("appSession").toString());
			oonlineShop.setRefreshToken(jsonObject.get("refreshToken")
					.toString());
			oonlineShop.setRefreshUpdate(jsonObject.get("refreshUpdate")
					.toString());
			oonlineShop.setCreatename(jsonObject.get("createname").toString());
			oonlineShop.setStrsCreatedate(ExtendDate.getYMD_h_m_s(new Date()));

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		SysDao.getOonlineShopBuz().add(oonlineShop);
		// String result = JsonHelper.toJSONString(keyid);

		return "{\"result\":\"" + keyid + "\"}";
	}

	public String modify(String jsonstr) {
		String result = "";
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		com.thinkin.ordercenter.po.OonlineShop oonlineShop = new com.thinkin.ordercenter.po.OonlineShop();
		try {
			result = jsonObject.get("keyid").toString();
			oonlineShop.setKeyid(jsonObject.get("keyid").toString());
			oonlineShop.setChannelKeyid(jsonObject.get("channelKeyid")
					.toString());
			oonlineShop.setPlatformID(jsonObject.get("platformID").toString());
			oonlineShop
					.setNetshopType(jsonObject.get("netshopType").toString());
			oonlineShop.setNetShopUserName(jsonObject.get("netShopUserName")
					.toString());
			oonlineShop
					.setNetShopName(jsonObject.get("netShopName").toString());
			oonlineShop.setNetShopUrl(jsonObject.get("netShopUrl").toString());
			// oonlineShop.setFileQueue(jsonObject.get("fileQueue").toString());
			oonlineShop.setAppKey(jsonObject.get("appKey").toString());
			oonlineShop.setAppSecret(jsonObject.get("appSecret").toString());
			oonlineShop.setAppSession(jsonObject.get("appSession").toString());
			oonlineShop.setRefreshToken(jsonObject.get("refreshToken")
					.toString());
			oonlineShop.setRefreshUpdate(jsonObject.get("refreshUpdate")
					.toString());
			oonlineShop.setModifyname(jsonObject.get("modifyname").toString());
			oonlineShop.setStrsModifydate(ExtendDate.getYMD_h_m_s(new Date()));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		SysDao.getOonlineShopBuz().modify(oonlineShop);
		return "{\"result\":\"" + result + "\"}";
	}

	public String delete(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String keyid = "";
		String result = "{'msg':'删除成功'}";
		try {
			keyid = jsonObject.get("keyid").toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		SysDao.getOonlineShopBuz().delete(keyid);
		return "{\"result\":\"删除成功\"}";
	}

	public String findQuery(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		OonlineShopVo qvo = new OonlineShopVo();
		String result = "{}";
		try {
			qvo.setNetShopName(jsonObject.getString("netShopName").toString());
			qvo.setPlatformID(jsonObject.getString("platformID").toString());
			qvo.setNetshopType(jsonObject.getString("netshopType").toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		List<com.thinkin.ordercenter.po.OonlineShop> oonlineShopLs = SysDao
				.getOonlineShopBuz().query(qvo);
		if (oonlineShopLs != null && oonlineShopLs.size() > 0) {
			result = JsonHelper.toJSONString(oonlineShopLs);
		}
		return result;
	}

	public String loadEntity(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String keyid = "";
		try {
			keyid = jsonObject.get("keyid").toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		com.thinkin.ordercenter.po.OonlineShop oonlineShop = SysDao
				.getOonlineShopBuz().loadEntity(keyid);
		String result = JsonHelper.toJSONString(oonlineShop);
		return result;
	}
}
